
### Data

PBS_data <- read.csv2("PBS_zbiór.csv", sep = ",")

PBS_data <- as.data.table(PBS_data)

PBS_data$waga <- as.numeric(PBS_data$waga)

### Creating variables for t-tests and means

### Treatment status rekod

PBS_data[, wariant := fcase (wariant == "Z interwencja", 1 ,
                             wariant == "Bez interwencji", 0)]

### Gender 

PBS_data[, M1 := ifelse(M1 == "Mezczyzna", 1, 0)]

### Age

PBS_data[, age := 2021 - Rok_urodzenia]

### Education level

PBS_data[, M3_rekod := fcase(M3_rekod == "Niskie", 1,
                             M3_rekod == "Srednie", 2,
                             M3_rekod == "Wyzsze\xa0", 3
)]

### Size of place of residence

PBS_data[, M5 := fcase (M5 == "Wies", 1,
                        M5 == "Male miasto (ponizej 20 tys. mieszkanc\xf3w)", 2,
                        M5 == "Srednie miasto (100-500 tys. mieszkanc\xf3w)", 3,
                        M5 == "Miasto (20-100 tys. mieszkanc\xf3w)", 4,
                        M5 == "Duze miasto (powyzej 500 tys. mieszkanc\xf3w)", 5)]

### Economic literacy 

PBS_data[, M6 := fcase (M6 == "Duza", 5,
                        M6 == "Raczej duza", 4,
                        M6 == "Srednia", 3,
                        M6 == "Raczej mala", 2,
                        M6 == "Mala", 1)]

### Size of household 

PBS_data[, .N, keyby = .(M13)]

### Level of individual income 

PBS_data[, M15 := fcase (M15 == "Do 1000 zl", 1,
                         M15 == "Od 1001 do 2000 zl", 2,
                         M15 == "Od 2001 do 3000 zl", 3,
                         M15 == "Od 3001 do 4000 zl", 4,
                         M15 == "Od 4001 do 5000 zl", 5,
                         M15 == "Od 5001 do 6000 zl ", 6,
                         M15 == "Od 6001 do 8000 zl", 7,
                         M15 == "Powyzej 8000 zl", 8
)]

### Level of household income

PBS_data[, M16 := fcase (M16 == "Do 1000 zl", 1,
                         M16 == "Od 1001 do 2000 zl", 2,
                         M16 == "Od 2001 do 3000 zl", 3,
                         M16 == "Od 3001 do 4000 zl", 4,
                         M16 == "Od 4001 do 5000 zl", 5,
                         M16 == "Od 5001 do 6000 zl ", 6,
                         M16 == "Od 6001 do 9000 zl", 7,
                         M16 == "Od 9001 do 12000 zl", 8,
                         M16 == "Powyzej 12000 zl", 9
)]

### Level of household income (per person)

PBS_data[, M16_p := M16/M13]

### Participation in national elections

PBS_data[, M17 := fcase(M17 == "Tak", 1, 
                        M17 == "Nie", 0)]
### Support for government

PBS_data[, M_20 := fifelse(M18 == "Prawo i Sprawiedliwosc", 1, 
                           0)]

### Not supporting government

PBS_data[, M_21 := fifelse(M18 != "Prawo i Sprawiedliwosc", 1, 
                           0)]
### Activity status


PBS_data[, M11 := fcase (M11_1 == "Wskazano", "Work",
                         M11_2 == "Wskazano", "Maternity or parental leave",
                         M11_3 == "Wskazano" | M11_4 == "Wskazano", "Professional inactivity",
                         M11_5 == "Wskazano", "In education",
                         M11_6 == "Wskazano", "Pension"
)]

### Binary for not working

PBS_data[, M11_3_b := ifelse(M11 == "Professional inactivity", 1, 0)]

### Binary for employment

PBS_data[, M11_1_b := ifelse(M11 == "Work", 1, 0)]

### Binary for pension

PBS_data[, M11_6_b := ifelse(M11 == "Pension", 1, 0)]

### Binary for parental or maternity leave

PBS_data[, M11_2_b := ifelse(M11 == "Maternity or parental leave", 1, 0)]

### Type of employment 

PBS_data[, M12 := fcase (M12_1 == "Wskazano", "Contract work",
                         M12_2 == "Wskazano", "Self-employment",
                         M12_3 == "Wskazano", "Agricultural business"
)]

### Binary for self-employed 

PBS_data[, M12_2_b := ifelse(M12 == "Self-employment", 1, 0)]

### Student

PBS_data[, M11_5_b := ifelse(M11 == "In education", 1, 0)]

### Survey design 

srs_design <- svydesign(id=~nr_wyw, weights=~waga, data=PBS_data)

### Gender

svyby(~M1, ~wariant, srs_design, svymean, na = TRUE)

svyby(~M1, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M1~wariant, srs_design, na = TRUE)

### Age 

svyby(~age, ~wariant, srs_design, svymean, na = TRUE)

svyttest(age~wariant, srs_design, na = TRUE)

### Education

svyby(~M3_rekod, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M3_rekod~wariant, srs_design, na = TRUE)

### Size of place of residence

svyby(~M5, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M5~wariant, srs_design, na = TRUE)

### Economic literacy 

svyby(~M6, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M6~wariant, srs_design, na = TRUE)

### Size of household 

svyby(~M13, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M13~wariant, srs_design, na = TRUE)

### Individual income

svyby(~M15, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M15~wariant, srs_design, na = TRUE)

### Household income per capita

svyby(~M16_p, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M16_p~wariant, srs_design, na = TRUE)

### Participation in elections

svyby(~M17, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M17~wariant, srs_design, na = TRUE)

### Support for government 

svyby(~M_20, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M_20~wariant, srs_design, na = TRUE)

### Not supporting government 

svyby(~M_21, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M_21~wariant, srs_design, na = TRUE)

### Unemployed

svyby(~M11_3_b, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M11_3_b~wariant, srs_design, na = TRUE)

### Employed

svyby(~M11_1_b, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M11_1_b~wariant, srs_design, na = TRUE)

### Self-employed

svyby(~M12_2_b, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M12_2_b~wariant, srs_design, na = TRUE)

### Student

svyby(~M11_5_b, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M11_5_b~wariant, srs_design, na = TRUE)
s
### Pension

svyby(~M11_6_b, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M11_6_b~wariant, srs_design, na = TRUE)

### Maternity or parental leave

svyby(~M11_2_b, ~wariant, srs_design, svymean, na = TRUE)

svyttest(M11_2_b~wariant, srs_design, na = TRUE)


